import commonConstants from "@/common/commonConstants";
import commonMethod from "@/common/commonMethod";
import { CallbackProperty, Cartesian3, Color, ColorMaterialProperty, HeadingPitchRoll, Math, Transforms } from "cesium";

class EffectManager{
    constructor(v,configs){
       this.effectMap = new Map();
       this.curViewer = v;
    }

    /**
     * 通过Key获得效果,工厂
     * @param regKey 效果注册的key
     * @param  binding 需要绑定的对象，可以是点、兵力或entity等
     * @param param 效果的参数设置
     */
    buildEffect(regKey,binding,param){
        let effect;
        
        if(regKey == commonConstants.effectType.PHASED_ARRAY_RADAR){
            effect = new PhRadar(this.curViewer,binding,param);
        }

        this.effectMap.set(effect.getID(),effect);
        return effect;
    }
    destoryEffect(effect){
        effect.destory();
        this.effectMap.delete(effect.getID())
    }
}

/**
 * 这是效果：大小变换
 * @constructor
 */
class DownSizingSketch {
    
    constructor(binding,opts){
        this.effectType=CommonConstants.effectType.DOWNSIZING;
        this.scale = 0.5;
        this.effectId = commonMethod.creatUUID()
        
        this.localScale = undefined;
        this.bindingEntity = binding;
        this.options = opts;
    }

    getID(){
        return this.effectId;
    }
   
    /**
     * 获得效果的类型
     */
    getType = function () {
        return this.effectType;
    };
   
     /**
     * 摧毁效果的方法
     */
     destory=function () {

     };
   
    /**
     * 效果单独使用可以在这里绑定位置或其他参数
     */
    render = function () {
        if (options){
            scale = options.scale;
        }
        bindingEntity.model.minimumPixelSize = localScale * scale;
    }
}


/**
 * 相控阵雷达效果
 */
class PhRadar{

    constructor(v, binding,params){
        this.sanPanes = new Map();
        this.sanDatas = [];
        this.radarPosition=undefined ;
        this.baoLuo = undefined ;
        this.radii = 90000;
        this.height = this.radii;
        this.myColor = Color.RED.withAlpha(0.3);
        this.outlineColor = Color.WHITE.withAlpha(0.7);
        this.mySanColor = Color.RED.withAlpha(0.8);
        this.show = false;
        this.automatic = false;
        this.step = 0.05;

        this.effectId = commonMethod.creatUUID();
        this.effectType = commonConstants.effectType.PHASED_ARRAY_RADAR;
        this.viewer = v;
        this.bindingEntity = binding;
        this.setOption(params)
    }

    getID(){
        return this.effectId;
    }

    getType = function () {
        return this.effectType;
    };

    setShow =function (bool) {
        this.baoLuo.show = bool;
        for (let sanpanleID of this.sanPanes.keys()){
            let sanpanle = this.sanPanes.get(sanpanleID);
            sanpanle.show = bool;
        }
       this.show = bool;
    };

    destory = function () {
        this.viewer.entities.remove(baoLuo);
        for (let sanPanel of this.sanPanes){
            this.viewer.entities.remove(sanPanel[1]);
        }
    };

    setOption(options) {
        if(options){
            if (options["radarPosition"]){
                this.radarPosition = options["radarPosition"];
            }
    
            if (options["radii"]){
                this.radii = options["radii"];
            }
    
            if (options["height"]){
                this.height = options["height"];
            }
    
            if (options["outlineColor"]){
                this.outlineColor=options["outlineColor"];
            }
    
            if (options["step"]){
                this.step = options["step"];
            }
    
            if (options["automatic"]){
                this.automatic = options["automatic"];
            }
    
            if (options["sanPanels"]){
                this.sanDatas = options["sanPanels"];
            }
    
            if (options["show"]){
                this.show = options["show"];
            }
        }
      
    }

    /**
     * 对兵力的封装
     */
    renderEffect() {
        const _this = this;
        let pos = _this.bindingEntity;
        if(_this.bindingEntity.className && _this.bindingEntity.className == "Troop"){
            pos = this.bindingEntity.getStaticPosition();
        }
      
        this.render({
            radarPosition:pos,
            radii:90000,
            height:90000,
            sanPanels:[{
                id : 1,
                heading : 0.0,
                range:{
                    max:120.0,
                    min:0.0
                }
            },{
                id : 2,
                heading : 120.0,
                range:{
                    max:240.0,
                    min:120.0
                }
            },{
                id : 3,
                heading : 240.0,
                range:{
                    max:360.0,
                    min:240.0
                }
            }],
            step:0.1,
            automatic:true,
            show:true
        })
    };

    dynamic () {
        this.baoLuo.position = this.bindingEntity.getEntity().position;
        for (let key of this.sanPanes.keys()){
            let sanPanel = this.sanPanes.get(key);
            sanPanel.position =  this.bindingEntity.getEntity().position;
        }
    };

    gc() {
        this.baoLuo.position = this.bindingEntity.getEntity().position;
        for (let key of this.sanPanes.keys()){
            let sanPanel = this.sanPanes.get(key);
            sanPanel.position =  this.bindingEntity.getEntity().position;
        }
    }

    render(op) {

        this.setOption(op);
        //包络的生成
        this.baoLuo = this.viewer.entities.add({
            position: this.radarPosition,
            ellipsoid:{
                radii: new Cartesian3(this.radii,this.radii,this.height),
                maximumCone: Math.PI_OVER_TWO,
                outlineColor: this.outlineColor,
                outlineWidth: 3,
                outline:true,
                material : new ColorMaterialProperty(this.myColor),
                slicePartitions :80,
                show: this.show
            },
        });

        //生成扇形扫描片
        for (let i = 0; i < this.sanDatas.length; i++) {
            let sanData = this.sanDatas[i];
            let sanPanel = this.createSanPanel(sanData);
            this.sanPanes.set(sanPanel.id,sanPanel);
        }

        return this;
    };

    /**
     * 根据传入的数据，生成扫描片
     * @param sanData
     */
    createSanPanel(sanData) {
        let heading = sanData["heading"] ;
        heading = heading?Number.parseFloat(heading):0.0;
        let roll = sanData["roll"];
        roll = roll?Number.parseFloat(roll):0.0;
        let picth = sanData["pitch"];
        picth = picth?Number.parseFloat(picth):0.0;

        let headingPitchRange = new HeadingPitchRoll(Math.toRadians(heading), Math.toRadians(picth), Math.toRadians(roll));

        let headingPitchRollQuaternion = Transforms.headingPitchRollQuaternion(this.radarPosition, headingPitchRange);
        if (this.automatic){
            let range = sanData["range"];
            headingPitchRollQuaternion = new CallbackProperty(e=>{
                    heading += this.step;
                    if (heading > range.max || heading < range.min){
                        this.step *= -1;
                    }
                    //循环而不是往返
                    /*if (heading >360){
                        heading = 0;
                    }*/
                    headingPitchRange.heading = Math.toRadians(heading);
                    return Transforms.headingPitchRollQuaternion(this.radarPosition, headingPitchRange);}
                ,false);
        }

        return this.viewer.entities.add({
            id:sanData.id,
            position: this.radarPosition,
            orientation: headingPitchRollQuaternion,
            ellipsoid: {
                radii: new Cartesian3(this.radii, this.radii, this.height),
                innerRadii: new Cartesian3(0.001, 0.001, 0.001),
                minimumClock: Math.toRadians(-0.001),
                maximumClock: Math.toRadians(0.001),
                minimumCone: Math.toRadians(75),
                maximumCone: Math.toRadians(90),
                material: new ColorMaterialProperty(this.mySanColor),
                fill:true,
                outline: false,
                show: this.show
            },
        });
    }

    /**
     * 手动刷新扇形片
     * @param json
     */
    flushPhRadar = function(json) {
        for (let i = 0; i <json.length; i++) {
            let singlePanel = json[i];
            this.flushSingleSanPanel(singlePanel);
        }
    };

    /**
     * 单独的刷新一个扫描片
     * @param singlePanelData
     */
    flushSingleSanPanel(singlePanelData) {
        let sanPanel =  this.sanPanes.get(singlePanelData.id);

        let heading = singlePanelData["heading"] ;
        heading = heading?Number.parseFloat(heading):0.0;
        let roll = singlePanelData["roll"];
        roll = roll?Number.parseFloat(roll):0.0;
        let picth = singlePanelData["pitch"];
        picth = picth?Number.parseFloat(picth):0.0;

        sanPanel.orientation = Transforms.headingPitchRollQuaternion(this.radarPosition,
            new HeadingPitchRoll(Math.toRadians(heading), Math.toRadians(picth), Math.toRadians(roll)));
    }
}



export default EffectManager;